package com.woniuxy.controller;

import java.util.HashMap;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.entity.Orders;
import com.woniuxy.entity.PageBean;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.User;
import com.woniuxy.service.CoachService;
import com.woniuxy.service.MessageService;
import com.woniuxy.service.OrdersService;
import com.woniuxy.util.CommonUtil;
/**
 * 
 * @author lishuwei&zenglingzhong
 *
 */
@Controller
@RequestMapping("/order")
public class OrdersController {
	@Autowired
	private OrdersService ordersService;
	@Autowired
	private MessageService messageService; 
	@Autowired
	private CoachService coachService;
	
	@RequestMapping("/mystus")
	@ResponseBody
	public ResponseEntity<PageBean<User>> getCoachStus(int page){
		ResponseEntity<PageBean<User>> re = new ResponseEntity<>();
		try {
			User nowUser = CommonUtil.getCurrentUser();
			PageBean<User> stus =  ordersService.getCoachStusByPage(nowUser.getId(),page);
			re.setCode(1);
			re.setData(stus);
		} catch (Exception e) {
			e.printStackTrace();
			re.setCode(2);
		}
		
		return re;
	}
	
	@RequestMapping("/getOrders")
	@ResponseBody
	public ResponseEntity<PageBean<Orders>> getOrders(int page){
		ResponseEntity<PageBean<Orders>> responseEntity = new ResponseEntity<>();
		PageBean<Orders> orders = new PageBean<Orders>();
		try {
			User nowUser = CommonUtil.getCurrentUser();
			if ("2".equals(nowUser.getRole())) {
				orders =  ordersService.getOrdersByCidByPage(nowUser.getId(),page);
				System.out.println(orders);
				responseEntity.setCode(2);
				responseEntity.setData(orders);
			}else if ("1".equals(nowUser.getRole())) {
				orders =  ordersService.getOrdersBySidByPage(nowUser.getId(),page);
				System.out.println(orders);
				responseEntity.setCode(1);
				responseEntity.setData(orders);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
			responseEntity.setCode(200);
		}
		
		return responseEntity;
	}
	
	/**
	 * 
	 * @param oid
	 * @param mid
	 * @return 1成功，2其他异常，3账户余额不足，4订单已支付
	 */
	@RequestMapping("/pay")
	@ResponseBody
	public ResponseEntity<Void> payOrder(Integer oid){
		ResponseEntity<Void> responseEntity = new ResponseEntity<>();
		try {
			int result = messageService.payOrder(oid);
			if(result==3) {
				responseEntity.setCode(3);
				return responseEntity;
			}
			if(result==4) {
				responseEntity.setCode(4);			
				return responseEntity;
			}			
			responseEntity.setCode(1);
		} catch (Exception e) {
			e.printStackTrace();
			responseEntity.setCode(2);
		}
		
		return responseEntity;
	}
	

	@RequestMapping("/acceptOrder")
	@ResponseBody
	public ResponseEntity<Void> acceptOrder(Integer oid,Integer mid){
		ResponseEntity<Void> entity = new ResponseEntity<>();
		User nowUser  = CommonUtil.getCurrentUser();
		try {
			int result = messageService.handleOrder(oid,"2",nowUser.getRole());
			if(result==3) {
				entity.setCode(3);//表示该申请已被处理
				return entity;
			}		
			entity.setCode(1);
		} catch (Exception e) {
			e.printStackTrace();
			entity.setCode(2);
		}		
		return entity;
	}
	
	
	@RequestMapping("/refuseOrder")
	@ResponseBody
	public ResponseEntity<Void> refuseOrder(Integer oid){
		User nowUser  = CommonUtil.getCurrentUser();
		ResponseEntity<Void> entity = new ResponseEntity<>();
		
		try {
			Orders order=ordersService.getOrdersByOid(oid);
			if (order.getChooseId()!=0) {
				ordersService.releaseCourse(order.getChooseId());
			}		
			int result = messageService.handleOrder(oid,"4",nowUser.getRole());
			if(result==3) {
				entity.setCode(3);
				return entity;
			}
			
			entity.setCode(1);
		} catch (Exception e) {
			e.printStackTrace();
			entity.setCode(2);
		}
		return entity;
	}
	@RequestMapping("getDealInfo")
	@ResponseBody
	public ResponseEntity<Map<String, Integer>> getDealInfo(Integer id) {
		
		ResponseEntity<Map<String, Integer>> entity = new ResponseEntity<>();
		Map<String, Integer> map=new HashMap<String, Integer>();
		
		User user=coachService.getUserById(id);
		System.err.println(user.getRole());
		try {
			int successNum=ordersService.getOrderNumById(id,true);
			int failNum=ordersService.getOrderNumById(id,false);
			int goodMark=0;
			int middleMark=0;
			int badMark=0;
			if ("2".equals(user.getRole())) {
				 goodMark=ordersService.getCoachMarkNum(id,1);
				 middleMark=ordersService.getCoachMarkNum(id,2);
				 badMark=ordersService.getCoachMarkNum(id,3);
			}else if("1".equals(user.getRole())) {
				 goodMark=ordersService.getStudentMarkNum(id,1);
				 middleMark=ordersService.getStudentMarkNum(id,2);
				 badMark=ordersService.getStudentMarkNum(id,3);
			}			
			map.put("successNum", successNum);
			map.put("failNum", failNum);
			map.put("goodMark", goodMark);
			map.put("middleMark", middleMark);
			map.put("badMark", badMark);
			entity.setData(map);
			entity.setCode(200);
		} catch (Exception e) {
			e.printStackTrace();
			entity.setCode(400);
			return entity;
		}		
		return entity;
		
	}
	
}
